/*
** xfce-fake.c
*/

#include "xfce-fake.h"


#define XFCE_FAKE_GROUP_NAME "XFCE_FAKE"


XfceRc*  xfce_rc_simple_open  (const gchar *filename, gboolean readonly)
{
    XfceRc * file = g_slice_new(XfceRc);
    file->file = g_key_file_new();
    if ( g_file_test(filename,G_FILE_TEST_EXISTS) == FALSE ){
	char * dirname = g_path_get_dirname(filename);
	if ( G_LIKELY(dirname) ){
	    g_mkdir_with_parents(dirname,0700);
	    g_free(dirname);
	}
    }
    g_key_file_load_from_file(file->file,filename,G_KEY_FILE_NONE,NULL);
    file->filename = readonly ? NULL : g_strdup(filename) ;
    file->group_name= g_strdup(XFCE_FAKE_GROUP_NAME);
    return file ;
}

gboolean            xfce_rc_read_bool_entry             (const XfceRc *rc,
                                                         const gchar *key,
                                                         gboolean fallback)
{
    GError * error = NULL;
    gboolean ret = g_key_file_get_boolean(rc->file,rc->group_name,key,&error);
    if ( ret == FALSE && error != NULL ){
	ret=fallback;
	g_error_free(error);
    }
    return ret;
}

gint                xfce_rc_read_int_entry              (const XfceRc *rc,
                                                         const gchar *key,
                                                         gint fallback)
{
    GError * error = NULL;
    gint ret = g_key_file_get_integer(rc->file,rc->group_name,key,&error);
    if ( ret == 0 && error != NULL){
	g_error_free(error);
	ret=fallback;
    }
    return ret;
}


#ifdef ROX_PLUGIN_FAKE_FRAMEBOX
GtkWidget *
xfce_create_framebox(const gchar *title,
                     GtkWidget **frame_bin)
{
    GtkWidget *framebox;

    g_return_val_if_fail(frame_bin, NULL);

    framebox = gtk_frame_new(NULL);
    gtk_frame_set_shadow_type(GTK_FRAME(framebox), GTK_SHADOW_NONE);
    gtk_frame_set_label_align(GTK_FRAME(framebox), 0.0, 1.0);
    gtk_widget_show(framebox);

    if(title) {
        gchar *tmp = g_strdup_printf("<b>%s</b>", title);
        GtkWidget *label = gtk_label_new(tmp);
        gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
        gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
        gtk_widget_show(label);
        gtk_frame_set_label_widget(GTK_FRAME(framebox), label);
        g_free(tmp);
    }

    *frame_bin = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
    gtk_alignment_set_padding(GTK_ALIGNMENT(*frame_bin), 5, 5, 21, 5);
    gtk_widget_show(*frame_bin);
    gtk_container_add(GTK_CONTAINER(framebox), *frame_bin);

    return framebox;
}

GtkWidget* 
xfce_create_framebox_with_content (const gchar *title, GtkWidget *content)
{
  GtkWidget* bin1;
  GtkWidget* outer1;
      
  outer1 = xfce_create_framebox (title, &bin1);
  
  gtk_container_add (GTK_CONTAINER (bin1), content);
        
  return outer1;
}

#endif


#ifdef ROX_PLUGIN_FAKE_XFCE_HEADER
static void
private_cb_eventbox_style_set (GtkWidget * widget, GtkStyle * old_style)
{
    static gboolean recursive = 0;
    GtkStyle *style;

    if (recursive > 0)
	return;

    ++recursive;
    style = gtk_widget_get_style (widget);
    gtk_widget_modify_bg (widget, GTK_STATE_NORMAL,
			  &style->bg[GTK_STATE_SELECTED]);
    --recursive;
}

static void
private_cb_label_style_set (GtkWidget * widget, GtkStyle * old_style)
{
    static gboolean recursive = 0;
    GtkStyle *style;

    if (recursive > 0)
	return;

    ++recursive;
    style = gtk_widget_get_style (widget);
    gtk_widget_modify_fg (widget, GTK_STATE_NORMAL,
			  &style->fg[GTK_STATE_SELECTED]);
    --recursive;
}


GtkWidget*
xfce_create_header_with_image (GtkWidget   *image,
                               const gchar *text)
{
    GtkWidget *eventbox, *label, *hbox;
    GtkStyle *style;
    char *markup;

    eventbox = gtk_event_box_new ();
    gtk_widget_show (eventbox);

    hbox = gtk_hbox_new (FALSE, 12);
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
    gtk_widget_show (hbox);
    gtk_container_add (GTK_CONTAINER (eventbox), hbox);

    if (image)
    {
	gtk_widget_show (image);
	gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
    }

    style = gtk_widget_get_style (eventbox);
    gtk_widget_modify_bg (eventbox, GTK_STATE_NORMAL,
			  &style->bg[GTK_STATE_SELECTED]);

    markup =
	g_strconcat ("<span size=\"larger\" weight=\"bold\">", text,
		     "</span>", NULL);
    label = gtk_label_new (markup);
    g_free (markup);
    gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
    gtk_widget_show (label);
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
    
    style = gtk_widget_get_style (label);
    gtk_widget_modify_fg (label, GTK_STATE_NORMAL,
			  &style->fg[GTK_STATE_SELECTED]);

    g_signal_connect_after (G_OBJECT (eventbox), "style_set",
			    G_CALLBACK (private_cb_eventbox_style_set), NULL);
    g_signal_connect_after (G_OBJECT (label), "style_set",
			    G_CALLBACK (private_cb_label_style_set), NULL);

    return eventbox;
}


GtkWidget*
xfce_create_header (GdkPixbuf *icon, const gchar *text)
{
    GtkWidget *image = NULL;
    
    if (icon)
	image = gtk_image_new_from_pixbuf (icon);

    return xfce_create_header_with_image (image, text);
}
#endif
